

namespace NodeBasedSkillEditor.Model.Node.RT
{
    public class Node_And_Or_RT : RT_Node
    {
        public RT_SingleValuePort Logic_RT;

        public RT_SingleValuePort[] Values_RT;

        public RT_SingleValuePort Result_RT;

        public Node_And_Or_RT(Node_And_Or sourceData, RT_DAG parentDag) : base(sourceData, parentDag)
        {
            Logic_RT = new RT_SingleValuePort(EPortIOType.Input, sourceData.Logic, this);

            Values_RT = BuildRTPortArrayFromGroup<RT_SingleValuePort>(EPortIOType.Input, sourceData.Values);

            Result_RT = new RT_SingleValuePort(EPortIOType.Output, sourceData.Result, this);
            Result_RT.OnGetValue = GetResult;
        }

        public AValue GetResult(DAG_Executor executor)
        {
            Result_RT.SourceValue.Reset();
            EAndOr andOr = (EAndOr)Logic_RT.GetValue(executor).ToEnum().Item2;

            if (Values_RT.Length > 0)
            {
                bool result = Values_RT[0].GetValue(executor).ToBoolean().Item2;

                for (int i = 1; i < Values_RT.Length; i++)
                {
                    var currBool = Values_RT[i].GetValue(executor).ToBoolean().Item2;
                    switch (andOr)
                    {
                        case EAndOr.And:
                            result = result && currBool;
                            break;
                        case EAndOr.Or:
                            result = result || currBool;
                            break;
                        default:
                            break;
                    }
                }

                Result_RT.SourceValue.SetBoolean(result);
            }

            return Result_RT.SourceValue;
        }
    }
}